package com.linghang.wusthelper.spider.jwc;

import com.linghang.wusthelper.dao.primary.AnnouncementDao;
import com.linghang.wusthelper.entity.primary.Announcement;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * @author origin
 * 教务处公告爬虫
 */
public class JwcAnnouncementSpider implements PageProcessor {


    private AnnouncementDao announcementDao;

    private Site site = Site.me().setRetryTimes(2).setTimeOut(3000)
            .addHeader("Referer", "http://jwc.wust.edu.cn/1925/list1.htm")
            .addHeader("Upgrade-Insecure-Requests", "1")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");

    public JwcAnnouncementSpider(AnnouncementDao announcementDao) {
        this.announcementDao = announcementDao;
    }

    @Override
    public void process(Page page) {
        int count = Integer.valueOf(page.getHtml().xpath("//*[@class='per_count']/text()").get());
        for (int i = 1; i <= count; i++) {
            String title = page.getHtml().xpath("//*[@class='news n" + i + " clearfix']/span[1]/a/@title").get();
            String url = page.getHtml().xpath("//*[@class='news n" + i + " clearfix']/span[1]/a/@href").get();
            String time = page.getHtml().xpath("//*[@class='news n" + i + " clearfix']/span[2]/text()").get();
            //  若数据库存在此数据，则不需要继续
            if (announcementDao.existTitle(title) > 0) {
                break;
            }

            //截取地址后四位 如果是html就再爬取内容
            String suffix1 = url.substring(url.length() - 4, url.length());
            //截取地址后三位 如果是htm就再爬取内容
            String suffix2 = url.substring(url.length() - 3, url.length());


            if (suffix1.equals("html") || suffix2.equals("htm")) {
                StringBuilder content = new StringBuilder();
                //  获取公告内容
                Spider.create(new JwcAnnContentSpider(content)).addUrl(url).thread(1).runAsync();
                synchronized (content) {
                    try {
                        content.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Announcement announcement = new Announcement();
                announcement.setAnnouncementTitle(title);
                announcement.setAnnouncementUrl(url);
                announcement.setAnnouncementCreatetime(time);
                announcement.setContent(content.toString());
                announcementDao.insert(announcement);
            } else {
                Announcement announcement = new Announcement();
                announcement.setAnnouncementTitle(title);

                // 下载文件显示修改
                String nurl1 = "<div style=\"margin-top: 300px;\">\n" +
                        "        <p align='center' style=\"color: rgb(128,199,131);\">";
                String nurl2 = "</p>\n" +
                        "        <a href=\"";
                String nurl3 = "\" style=\"margin: 0 auto;width:80px;height:80px;display: block;text-decoration: none;\">\n" +
                        "            \n" +
                        "            <div style=\"width: 100%;height: 100%;margin: 0 auto;background-repeat: no-repeat;background-size: 100%; background-image:url('')\"></div>\n" +
                        "        </a>\n" +
                        "    </div>";

                String nurl = nurl1 + title + nurl2 + url + nurl3;
                announcement.setAnnouncementUrl(url);
                announcement.setAnnouncementCreatetime(time);
                announcement.setContent(nurl);
                announcementDao.insert(announcement);
            }
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
